import plotly.graph_objects as go
import numpy as np

# 生成x数据，在[0,5]区间内取100个点
x = np.linspace(0, 5, 100)

# 根据函数关系计算y和z
y = x  # y = x
z = x  # z = x

# 创建3D散点图
fig = go.Figure()
fig.add_trace(
    go.Scatter3d(
        x=x,
        y=y,
        z=z,
        mode="lines",  # 使用线连接点
        line=dict(color="blue", width=5),
        marker=dict(
            size=3,
            color=z,  # 颜色根据z值变化
            colorscale="Viridis",
            opacity=0.8,
        ),
        name="y=x,z=x"
    )
)

# 设置图表布局
fig.update_layout(
    title="3D图形: z = x, y = x (x ∈ [0,5])",
    scene=dict(
        xaxis_title="X轴",
        yaxis_title="Y轴",
        zaxis_title="Z轴",
        # 设置坐标轴范围使图形比例更协调
        xaxis=dict(range=[0, 5]),
        yaxis=dict(range=[0, 5]),
        zaxis=dict(range=[0, 5]),
    ),
    margin=dict(l=0, r=0, b=0, t=40),
)

# 显示图形
fig.show()
